/*
 * ListTemperaFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.ruif.view;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.ruif.client.TemperaServiceImpl;
import com.ruif.model.Tempera;
import com.ruif.service.ITemperaService;
import com.ruif.util.DBUtil;
import com.ruif.util.StringUtil;

/**
 *
 * @author __USER__
 */
public class ListTemperaFrame extends javax.swing.JInternalFrame
{

    /**
     * 
     */
    private static final long serialVersionUID = 664971768900060757L;

    DBUtil dbUtil = new DBUtil();
    ITemperaService temService = new TemperaServiceImpl();

    /** Creates new form ListTemperaFrame */
    public ListTemperaFrame()
    {
        initComponents();
        this.setLocation(200, 50);
        fillDesktop(null);
    }

    //GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents()
    {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_list = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jTextField_id = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField_temp = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField_date = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton_query = new javax.swing.JButton();
        jButton_delete = new javax.swing.JButton();
        jButton_modify = new javax.swing.JButton();
        jComboBox_status = new javax.swing.JComboBox();
        jButton_clean = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);

        jTable_list.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[]
        { "编号", "温度", "日期", "状态" })
        {
            boolean[] canEdit = new boolean[]
            { false, false, false, false };

            public boolean isCellEditable(int rowIndex, int columnIndex)
            {
                return canEdit[columnIndex];
            }
        });
        jTable_list.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mousePressed(java.awt.event.MouseEvent evt)
            {
                jTable_listMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable_list);

        jLabel1.setText("\u7f16 \u53f7\uff1a");

        jTextField_id.setEditable(false);

        jLabel2.setText("\u6e29 \u5ea6\uff1a");

        jLabel3.setText("\u65e5 \u671f\uff1a");

        jLabel4.setText("\u72b6 \u6001\uff1a");

        jButton_query.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\pc\\桌面\\Images\\query.png")); // NOI18N
        jButton_query.setText("\u67e5\u8be2");
        jButton_query.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton_queryActionPerformed(evt);
            }
        });

        jButton_delete.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\pc\\桌面\\Images\\delete.png")); // NOI18N
        jButton_delete.setText("\u5220\u9664");
        jButton_delete.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton_deleteActionPerformed(evt);
            }
        });

        jButton_modify.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\pc\\桌面\\Images\\modify.png")); // NOI18N
        jButton_modify.setText("\u4fee\u6539");
        jButton_modify.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton_modifyActionPerformed(evt);
            }
        });

        jComboBox_status.setModel(new javax.swing.DefaultComboBoxModel(new String[]
        { "正常", "报警" }));

        jButton_clean.setIcon(new javax.swing.ImageIcon("F:\\WorkSpaces\\ruif_swing_utf8\\images\\clear.png")); // NOI18N
        jButton_clean.setText("\u6e05\u7a7a");
        jButton_clean.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton_cleanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addContainerGap()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(
                                                                                        jScrollPane1,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        469, Short.MAX_VALUE)
                                                                                .addGroup(
                                                                                        layout.createSequentialGroup()
                                                                                                .addGap(22, 22, 22)
                                                                                                .addGroup(
                                                                                                        layout.createParallelGroup(
                                                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                                .addGroup(
                                                                                                                        layout.createSequentialGroup()
                                                                                                                                .addGroup(
                                                                                                                                        layout.createParallelGroup(
                                                                                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                                                                                .addComponent(
                                                                                                                                                        jLabel1)
                                                                                                                                                .addComponent(
                                                                                                                                                        jLabel3))
                                                                                                                                .addPreferredGap(
                                                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                                .addGroup(
                                                                                                                                        layout.createParallelGroup(
                                                                                                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                                                                                                false)
                                                                                                                                                .addComponent(
                                                                                                                                                        jTextField_id)
                                                                                                                                                .addComponent(
                                                                                                                                                        jTextField_date,
                                                                                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                                        97,
                                                                                                                                                        Short.MAX_VALUE))
                                                                                                                                .addGap(18,
                                                                                                                                        18,
                                                                                                                                        18)
                                                                                                                                .addGroup(
                                                                                                                                        layout.createParallelGroup(
                                                                                                                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                                                                                false)
                                                                                                                                                .addGroup(
                                                                                                                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                                                                                                                        layout.createSequentialGroup()
                                                                                                                                                                .addComponent(
                                                                                                                                                                        jLabel2)
                                                                                                                                                                .addPreferredGap(
                                                                                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                                                                .addComponent(
                                                                                                                                                                        jTextField_temp,
                                                                                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                                                        109,
                                                                                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                                                                                                .addGroup(
                                                                                                                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                                                                                                                        layout.createSequentialGroup()
                                                                                                                                                                .addComponent(
                                                                                                                                                                        jLabel4)
                                                                                                                                                                .addPreferredGap(
                                                                                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                                                                .addComponent(
                                                                                                                                                                        jComboBox_status,
                                                                                                                                                                        0,
                                                                                                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                                                        Short.MAX_VALUE))))
                                                                                                                .addGroup(
                                                                                                                        layout.createSequentialGroup()
                                                                                                                                .addComponent(
                                                                                                                                        jButton_query)
                                                                                                                                .addGap(18,
                                                                                                                                        18,
                                                                                                                                        18)
                                                                                                                                .addComponent(
                                                                                                                                        jButton_clean,
                                                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                        85,
                                                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))))
                                                .addGroup(
                                                        layout.createSequentialGroup().addGap(34, 34, 34)
                                                                .addComponent(jButton_delete).addGap(18, 18, 18)
                                                                .addComponent(jButton_modify))).addContainerGap()));
        layout.setVerticalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addGap(48, 48, 48)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGap(2, 2, 2)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                .addComponent(jLabel1)
                                                                                .addComponent(
                                                                                        jTextField_id,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                .addGap(13, 13, 13)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                .addComponent(jLabel3)
                                                                                .addComponent(
                                                                                        jTextField_date,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                .addComponent(jLabel2)
                                                                                .addComponent(
                                                                                        jTextField_temp,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                .addGap(13, 13, 13)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                .addComponent(jLabel4)
                                                                                .addComponent(
                                                                                        jComboBox_status,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(18, 18, 18)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jButton_query)
                                                .addComponent(jButton_clean, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        25, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(50, 50, 50)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jButton_modify).addComponent(jButton_delete))
                                .addContainerGap(62, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>
    //GEN-END:initComponents

    private void jButton_cleanActionPerformed(java.awt.event.ActionEvent evt)
    {
        this.resetValue();
        this.fillDesktop(null);
    }

    /**
     * 触发修改功能的事件
     * @param evt
     */
    private void jButton_modifyActionPerformed(java.awt.event.ActionEvent evt)
    {
        String id = jTextField_id.getText().trim();
        String temp = jTextField_temp.getText().trim();
        String date = jTextField_date.getText().trim();
        int status = jComboBox_status.getSelectedIndex();

        if (StringUtil.isEmpty(id))
        {
            JOptionPane.showMessageDialog(null, "请选择要修改的记录！");
            return;
        }

        Tempera tem = new Tempera(Long.parseLong(id), Integer.parseInt(temp), date, status);

        int ok = temService.modify(tem);
        if (ok == 0)
        {
            return;
        }

        JOptionPane.showMessageDialog(null, "修改成功");
        this.resetValue();
        this.fillDesktop(null);
    }

    /**
     * 触发删除的功能事件
     * @param evt
     */
    private void jButton_deleteActionPerformed(java.awt.event.ActionEvent evt)
    {
        String id = jTextField_id.getText().trim();

        int ok = temService.delete(id);
        if (ok == 0)
        {
            return;
        }

        JOptionPane.showMessageDialog(null, "删除成功！");
        this.resetValue();
        this.fillDesktop(null);
    }

    /**
     * 重置
     */
    private void resetValue()
    {
        this.jTextField_id.setText("");
        this.jTextField_temp.setText("");
        this.jTextField_date.setText("");
        this.jComboBox_status.setSelectedItem("正常");
    }

    /**
     * table中触发 pressed事件效果
     * 
     * @param evt
     */
    private void jTable_listMousePressed(java.awt.event.MouseEvent evt)
    {
        int row = this.jTable_list.getSelectedRow();
        this.jTextField_id.setText(jTable_list.getValueAt(row, 0) + "");
        this.jTextField_temp.setText(jTable_list.getValueAt(row, 1) + "");
        this.jTextField_date.setText(jTable_list.getValueAt(row, 2) + "");
        this.jComboBox_status.setSelectedItem(jTable_list.getValueAt(row, 3) + "");
    }

    private void jButton_queryActionPerformed(java.awt.event.ActionEvent evt)
    {
        int temp = StringUtil.isEmpty(this.jTextField_temp.getText().trim()) ? 0 : Integer
                .parseInt(this.jTextField_temp.getText().trim());
        String date = this.jTextField_date.getText().trim();
        int status = this.jComboBox_status.getSelectedIndex();
        Tempera tem = new Tempera(temp, date == null ? "" : date, status);
        this.fillDesktop(tem);
    }

    /**
     * 查询 
     * @param tem
     */
    public void fillDesktop(Tempera tem)
    {
        DefaultTableModel dtm = (DefaultTableModel) jTable_list.getModel();
        dtm.setRowCount(0); // 设置成 0 行

        temService.list(dtm, tem);
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton jButton_clean;
    private javax.swing.JButton jButton_delete;
    private javax.swing.JButton jButton_modify;
    private javax.swing.JButton jButton_query;
    private javax.swing.JComboBox jComboBox_status;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable_list;
    private javax.swing.JTextField jTextField_date;
    private javax.swing.JTextField jTextField_id;
    private javax.swing.JTextField jTextField_temp;
    // End of variables declaration//GEN-END:variables

}